#include "rttADC.h"

#include <usdev/adc.h>

using namespace NXMFC;

rttADC::rttADC()
{
    vref = 0;
    sampmax = 0;
}

rttADC::~rttADC()
{
}

bool rttADC::open(const char *dev)
{
    struct rt_adc_info info;

    if (!rttIODevice::open(dev))
        return false;

    if (ioctl(ADC_GETINFO, &info) != 0)
    {
        close();
        return false;
    }

    this->vref = info.vref;
    this->sampmax = info.sampling_max;

    return true;
}

float rttADC::voltage(float factor)
{
    int32_t s;
    float val = 0;

    read(&s, 4);
    val = s * this->vref * factor / this->sampmax;

    return val;
}
